package com.ytlz.dmcs.config;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import com.ytlz.dmcs.model.system.dao.RoleDao;
import com.ytlz.dmcs.model.system.dao.SysFieldDao;
import com.ytlz.dmcs.model.system.dao.SysMenuDao;
import com.ytlz.dmcs.model.system.dao.SysObjectDao;
import com.ytlz.dmcs.model.system.dao.UserDao;
import com.ytlz.dmcs.model.system.entity.SysField;
import com.ytlz.dmcs.model.system.entity.SysMenu;
import com.ytlz.dmcs.model.system.entity.SysObject;
import com.ytlz.dmcs.model.system.entity.SysRole;
import com.ytlz.dmcs.model.system.entity.SysUser;

@Component
public class DataRunner implements ApplicationRunner {

	@Autowired
	private SystemConfiguration config;

	@Autowired
	private SysMenuDao sysMenuDao;

	@Autowired
	private UserDao userservice;

	@Autowired
	private RoleDao roleservice;

	@Autowired
	private SysObjectDao sysObjectDao;

	@Autowired
	private PasswordEncoder passwordEncoder;

	@Override
	public void run(ApplicationArguments args) throws Exception {
		System.out.println("===============================================");
		System.out.println("DATA RUNNER");

		initAdminUser(initRole());
		initCustomUser();
		initSysObject();
		initMenu();

		System.out.println("===============================================");

	}

	private SysRole initRole() {
		SysRole roleAdmin = roleservice.findByName("admin");
		if (roleAdmin == null) {
			roleAdmin = new SysRole("admin", "管理员角色");
			roleAdmin = roleservice.save(roleAdmin);
		}

		SysRole roleCustom = roleservice.findByName("custom");
		if (roleCustom == null) {
			roleCustom = new SysRole("custom", "一般用户角色");
			roleCustom = roleservice.save(roleCustom);
		}
		return roleAdmin;
	}

	private void initCustomUser() {
		SysRole role = roleservice.findByName("custom");
		SysUser customUser = userservice.findByUsername("custom");
		if (customUser == null) {
			customUser = new SysUser("custom", "一般用户", "custom@ytlz.com",
					passwordEncoder.encode(config.getDefaultPassword()));
			List<SysRole> roles = new ArrayList<SysRole>();
			roles.add(role);
			customUser.setRoles(roles);
			customUser.setCreateDate(new Date());
			customUser.setModifyDate(new Date());
			customUser = userservice.save(customUser);
		}
	}

	private void initAdminUser(SysRole role) {
		SysUser adminUser = userservice.findByUsername("admin");
		if (adminUser == null) {
			adminUser = new SysUser("admin", "系统管理员", "admin@ytlz.com",
					passwordEncoder.encode(config.getDefaultPassword()));
			List<SysRole> roles = new ArrayList<SysRole>();
			roles.add(role);
			adminUser.setRoles(roles);
			adminUser.setCreateDate(new Date());
			adminUser.setModifyDate(new Date());
			adminUser = userservice.save(adminUser);
		}

		adminUser = userservice.findByUsername("system");
		if (adminUser == null) {
			adminUser = new SysUser("system", "管理员", "system@ytlz.com",
					passwordEncoder.encode(config.getDefaultPassword()));
			List<SysRole> roles = new ArrayList<SysRole>();
			roles.add(role);
			adminUser.setRoles(roles);
			adminUser.setCreateDate(new Date());
			adminUser.setModifyDate(new Date());
			adminUser = userservice.save(adminUser);
		}
	}

	private void initSysObject() {
		long sysObjectCount = sysObjectDao.count();
		System.out.println("SysObject Count : " + sysObjectCount);
		if (sysObjectCount < 1) {

		} else {

		}
	}

	private void initMenu() {
		if (sysMenuDao.count() < 1) {
			SysMenu menu = new SysMenu("系统管理", "system");
			menu = sysMenuDao.save(menu);

			SysMenu submenu = new SysMenu("用户", "admin/user", menu.getId(), "system");
			submenu = sysMenuDao.save(submenu);
			submenu = new SysMenu("角色", "admin/role", menu.getId(), "system");
			submenu = sysMenuDao.save(submenu);
			submenu = new SysMenu("对象管理", "admin/object", menu.getId(), "system");
			submenu = sysMenuDao.save(submenu);

			menu = new SysMenu("数据管理", "system");
			menu = sysMenuDao.save(menu);

			submenu = new SysMenu("固定资产记录", "datalist?type=lt_assert_record", menu.getId(), "system");
			submenu = sysMenuDao.save(submenu);

		}
	}
}
